﻿/*****************************************************************//**
 * @file    FITKGraphObjectLoadBase.h
 * @brief   Graph object for load or BC base.
 *
 * @author  ChengHaotian (yeguangbaozi@foxmail.com)
 * @date    2024-04-10
 *********************************************************************/

#ifndef __FITKGRAPHOBJECTLOADBASE_H__
#define __FITKGRAPHOBJECTLOADBASE_H__

#include "FITKGraphObjectFixedSizeBase.h"

#include "FITKVTKGraphAdaptorAPI.h"

// Forward declaration
class vtkPolyDataAlgorithm;
class vtkPoints;
class vtkCellArray;
class vtkPolyData;
class vtkIntArray;
class vtkDoubleArray;
class vtkTransform;
class vtkSelectionNode;
class vtkSelection;
class vtkExtractSelection;
class vtkIdTypeArray;
class vtkUnstructuredGrid;

namespace Interface
{
    class FITKAbstractAssInstance;
    class FITKBoundaryLocation;
}

namespace Exchange
{
    /**
     * @brief   Graph object for fixed-sized load or BC base.
     * @author  ChengHaotian (yeguangbaozi@foxmail.com)
     * @date    2024-04-10
     */
    class FITKVTKGRAPHADAPTORAPI FITKGraphObjectLoadBase : public FITKGraphObjectFixedSizeBase
    {
        // Regist
        FITKGraphObjectRegist(FITKGraphObjectLoadBase, FITKGraphObjectFixedSizeBase);
        FITKCLASS(Exchange, FITKGraphObjectLoadBase);

    public:
        /**
         * @brief   Constructor.
         * @param   caseData: The case data
         * @param   load: The load data object
         * @param   stepId: The step id
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-10
         */
        FITKGraphObjectLoadBase(AbaqusData::FITKDataCase* caseData, Interface::FITKBoundaryLocation* load, int stepId = 0);

        /**
         * @brief   Destructor.[virtual]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-10
         */
        virtual ~FITKGraphObjectLoadBase();

        /**
         * @brief   Show or hide graph object.[virtual][override]
         * @param   visibility: The visibility of graph object
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-08
         */
        virtual void setVisible(bool visibility) override;

        /**
         * @brief   Highlight the load graph actors.[virtual][override]
         * @param   color: The given color for highlighting this time[default]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-10
         */
        virtual void highlight(QColor color = QColor()) override;

        /**
         * @brief   Dis-highlight the load graph actors.[virtual][override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-10
         */
        virtual void disHighlight() override;

        /**
         * @brief   Change the state by the given stepId.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-13
         */
        void changeStep(int stepId);

        /**
         * @brief   Update the load actors by current step.[virtual][override]
         * @param   forceUpdate: Force update[default]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-29
         */
        virtual void update(bool forceUpdate = false) override;

        /**
         * @brief   Update the visibility with data object's visibility.[virtual][override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        virtual void updateVisibility() override;

        /**
         * @brief   Simplified the graph object if necessary.[virtual][override]
         * @param   enabled: Enabled simplified flag
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-07
         */
        virtual void simplified(bool enabled) override;

        /**
         * @brief   If the graph object has relation with the given data object id.[virtual][override]
         * @param   dataObjId: The data object id
         * @return  Has relation
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-24
         */
        virtual bool isRelatedTo(int dataObjId) override;

        /**
         * @brief   Get the fixed sized bounds of this graph object.[virtual][override]
         * @param   The bounds size
         * @return  If the graph object has fixed sized bounds
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-29
         */
        virtual bool getFixedBounds(double* bounds) override;

    protected:
        /**
         * @brief   Constructor.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-01
         */
        FITKGraphObjectLoadBase() = default;

        /**
         * @brief   Initialize.[virtual]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-09
         */
        virtual void init();

        /**
         * @brief   Clear the poly data and filters after creating the load glyphs.[virtual][override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-13
         */
        virtual void clearCache() override;

        /**
         * @brief   Get value with the given step id.[virtual]
         * @param   stepId: The step id
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-25
         */
        virtual void getValue(int stepId);

        /**
         * @brief   Generate load actor.[virtual]
         * @param   src: The source data
         * @param   size: The actor size ( -1 means use default fixed size )[default]
         * @param   details: The additional information[default]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-10
         */
        virtual void generateGraph(vtkPolyDataAlgorithm* src, double size = -1., QVariant details = QVariant());

        /**
         * @brief   Insert a new point and its value to points and data.[virtual]
         * @param   pos: The position of point
         * @param   modelId: The model id of this point
         * @param   addToLoadPoints: Add the point to the load points data.[default]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-13
         */
        virtual void insertNextPoint(double* pos, int modelId, bool addToLoadPoints = true);

        /**
         * @brief   Generate the poly data for the force or moment load.[virtual]
         * @param   load: The force or moment load data object
         * @return  Is OK
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-29
         */
        virtual bool generatePolyData(Interface::FITKBoundaryLocation* load);

        /**
         * @brief   Get the visibility from the data object.[virtual][override]
         * @return  The visibility
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        virtual bool getDataVisibility() override;

        /**
         * @brief   Clear the model ids and the visibility hash.[virtual]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-24
         */
        virtual void clearModelsInfo();

        /**
         * @brief   Clear the data.[virtual]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-24
         */
        virtual void clearData();

        /**
         * @brief   Check if the data has been changed for additional data.( Such as coodinate. )[virtual]
         * @return  Has been changed
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-29
         */
        virtual bool checkDataChanged();

    protected:
        /**
         * @brief   The load glyph actor list.( For highlighting. )
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-29
         */
        QList<FITKGraphActor*> m_loadActorList;

        /**
         * @brief   The selection node for extract mesh grid.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        vtkSelectionNode* m_selectNode{ nullptr };

        /**
         * @brief   The selection for vtkSelectionNode.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        vtkSelection* m_section{ nullptr };

        /**
         * @brief   The mesh grid extractor.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        vtkExtractSelection* m_extractSelection{ nullptr };

        /**
         * @brief   The indice array of extract id.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        vtkIdTypeArray* m_idArray{ nullptr };

        /**
         * @brief   The step id.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-13
         */
        int m_stepId = 0;

        /**
         * @brief   Save the load's surface id.
         *          ( For updating load, while the load's surface has been changed. )
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-22
         */
        int m_surfaceId = -1;

        /**
         * @brief   Save the load's set id.
         *          ( For updating load, while the load's surface has been changed. )
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-25
         */
        int m_setId = -1;

        /**
         * @brief   The sub set ids of the comp set or surface.( the location of the load )
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-18
         */
        QList<int> m_subSetIds;

        /**
         * @brief   The input source data of arrow.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-10
         */
        QList<vtkPolyDataAlgorithm*> m_sourceList{};

        /**
         * @brief   The real points of the load or BC.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-13
         */
        vtkPoints* m_loadPoints{ nullptr };

        /**
         * @brief   The points data.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-10
         */
        vtkPoints* m_points{ nullptr };

        /**
         * @brief   The cells data.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-10
         */
        vtkCellArray* m_cells{ nullptr };

        /**
         * @brief   The poly data.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-10
         */
        vtkPolyData* m_polyData{ nullptr };

        ///**
        // * @brief   The points actor for highlighting.
        // * @author  ChengHaotian (yeguangbaozi@foxmail.com)
        // * @date    2024-04-24
        // */
        //FITKGraphActor* m_actorPoints{ nullptr };

        /**
         * @brief   The normal data.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-10
         */
        vtkDoubleArray* m_normal{ nullptr  };

        /**
         * @brief   The scalar data.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-10
         */
        vtkDoubleArray* m_scalar{ nullptr };

        /**
         * @brief   The model id array data.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        vtkIntArray* m_modelIdArray{ nullptr };

        /**
         * @brief   The default resolution of input arrow source.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-12
         */
        int m_sourceResolution = 6;

        /**
         * @brief   The line width of the glyph actor.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-10
         */
        double m_glyphLineWidth = 2.;

        /**
         * @brief   If the step changed.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-25
         */
        bool m_stepChanged = false;

        /**
         * @brief   All the object's parent models id, if the parent is invisible, then hide itself partly or fully.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        QList<int> m_modelIds;

        /**
         * @brief   The visibility of all parent models.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        QHash<int, bool> m_modelVisibilityHash;

    };
}   // namespace Exchange

#endif // __FITKGRAPHOBJECTLOADBASE_H__
